﻿using System;
using System.Security;
using System.Windows.Forms;
using HRM.DICTIONARY.DAL;
using EFERP;


namespace HRM.DICTIONARY.UI
{
    public partial class xfrmWorkAdd : DevExpress.XtraEditors.XtraForm
    {
        hrm_work _obj;
        readonly ICommon<hrm_work> _exeDic;
        readonly ICommon<hrm_employee> _exeDicEmployee;
        CloseOrNew _closeNew = CloseOrNew.Close;

        public xfrmWorkAdd(ICommon<hrm_work> exeDic)
        {
            InitializeComponent();
            _exeDicEmployee = new Common<hrm_employee>(StaticVariable.ERPEn);
            this._exeDic = exeDic;
            this._exeDic.myAction = Actions.Insert;
            this._obj = new hrm_work();
            SetDataIntoUI();
        }
        public xfrmWorkAdd(hrm_work currObj, ICommon<hrm_work> exeDic)
        {
            InitializeComponent();
            _exeDicEmployee = new Common<hrm_employee>(StaticVariable.ERPEn);
            this._exeDic = exeDic;
            this._exeDic.myAction = Actions.Update;
            this._obj = currObj;
            SetDataIntoUI();
        }
        private void xucWorkAdd_FormClosing(object sender, FormClosingEventArgs e)
        {
            TotalClose();
        }

        private void SetDataIntoUI()
        {
            if (_obj == null)
                return;
            switch (_exeDic.myAction)
            {
                case Actions.Update:
                    txtID.Text = _obj.workid.ToString();
                    txtNAME.Text = _obj.work;
                    cboPriority.EditValue = _obj.priority;
                    dtBeginDate.EditValue = _obj.begindate;
                    dtEndDate.EditValue = _obj.enddate;
                    cboStatus.EditValue = _obj.status;
                    calComplete.EditValue = _obj.complete;
                    calCost.EditValue = _obj.cost;
                    glkDeliverer.EditValue = _obj.deliverer;
                    glkEmployee.EditValue = _obj.employeecode;
                    txtDescription.Text = _obj.description;

                    btnLuuDong.Visible = true;
                    btnThoat.Visible = true;
                    btnLuuThem.Visible = false;
                    break;
                case Actions.Insert:
                    _obj = new hrm_work();
                    txtID.Text = _exeDic.NewID("WR");
                    txtNAME.Text = _obj.work;
                    cboPriority.EditValue = _obj.priority;
                    dtBeginDate.EditValue = _obj.begindate;
                    dtEndDate.EditValue = _obj.enddate;
                    cboStatus.EditValue = _obj.status;
                    calComplete.EditValue = _obj.complete;
                    calCost.EditValue = _obj.cost;
                    glkDeliverer.EditValue = _obj.deliverer;
                    glkEmployee.EditValue = _obj.employeecode;
                    txtDescription.Text = _obj.description;

                    btnLuuDong.Visible = true;
                    btnThoat.Visible = true;
                    btnLuuThem.Visible = true;
                    break;
                case Actions.InsertMultiple:
                    _obj = new hrm_work();
                    txtID.Text = _exeDic.NewID("WR");
                    txtNAME.Text = _obj.work;
                    cboPriority.EditValue = _obj.priority;
                    dtBeginDate.EditValue = _obj.begindate;
                    dtEndDate.EditValue = _obj.enddate;
                    cboStatus.EditValue = _obj.status;
                    calComplete.EditValue = _obj.complete;
                    calCost.EditValue = _obj.cost;
                    glkDeliverer.EditValue = _obj.deliverer;
                    glkEmployee.EditValue = _obj.employeecode;
                    txtDescription.Text = _obj.description;

                    btnLuuDong.Visible = true;
                    btnThoat.Visible = true;
                    btnLuuThem.Visible = true;
                    break;
            }
        }

        private bool SetUIIntoData()
        {
            if (txtID.Text == "" || txtNAME.Text == "" || txtDelivererName.Text=="" || txtEmployeeName.Text =="")
            {
                MessageBox.Show(@"Vui lòng không để trống thông tin công việc");
               
                return false;
            }
            _obj.workid = Guid.Parse(txtID.Text);
            txtNAME.Text = _obj.work;
            _obj.priority = cboPriority.EditValue.ToString();
            _obj.begindate = dtBeginDate.DateTime;
            _obj.enddate = dtEndDate.DateTime;
            _obj.status = cboStatus.SelectedIndex;

             _obj.complete = (double)calComplete.Value;
             _obj.cost = calCost.Value;
            _obj.deliverer = glkDeliverer.EditValue.ToString();
            _obj.employeecode = glkEmployee.EditValue.ToString();
            _obj.description = txtDescription.Text;
            return true;
        }
        private void TotalControl()
        {
            bool kq;
            switch (_exeDic.myAction)
            {
                case Actions.Insert:
                    kq = SetUIIntoData();
                    if (kq)
                    {
                        kq = _exeDic.InsertOnGridView(_obj);
                        if (kq)
                        {
                            this.DialogResult = DialogResult.OK;
                            _exeDic.myAction = Actions.Insert;
                            this.Close();
                        }
                        else
                        {
                            MessageBox.Show(@"Lỗi thêm dữ liệu vào hệ thống");
                        }
                    }
                    break;

                case Actions.Update:
                    kq = SetUIIntoData();
                    if (kq)
                    {
                        kq = _exeDic.UpdateOnGridView(_obj);
                        if (kq)
                        {
                            this.DialogResult = DialogResult.OK;
                            _exeDic.myAction = Actions.Update;
                            this.Close();
                        }
                        else
                        {
                            _exeDic.ValueOfGridViewRow = null;
                            MessageBox.Show(@"Lỗi cập nhật dữ liệu vào hệ thống");
                        }
                    }
                    break;
                case Actions.InsertMultiple:
                    kq = SetUIIntoData();
                    if (kq)
                    {
                        kq = _exeDic.InsertOnGridView(_obj);
                        if (kq)
                        {
                            _exeDic.myAction = Actions.InsertMultiple;
                            SetDataIntoUI();
                            this.DialogResult = DialogResult.OK;
                            if (_closeNew == CloseOrNew.Close)
                                this.Close();
                            else
                                _closeNew = CloseOrNew.New;
                        }
                        else
                        {
                            MessageBox.Show(@"Lỗi thêm dữ liệu vào hệ thống");
                        }
                    }
                    break;
            }
        }
        private void TotalClose()
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            TotalClose();
        }
        private void btnLuuDong_Click(object sender, EventArgs e)
        {
            _closeNew = CloseOrNew.Close;
            TotalControl();
        }
        private void btnLuuThem_Click(object sender, EventArgs e)
        {
            _exeDic.myAction = Actions.InsertMultiple;
            _closeNew = CloseOrNew.New;
            TotalControl();
        }

        private void glkDeliverer_EditValueChanged(object sender, EventArgs e)
        {
            hrm_employee objEmployee = _exeDicEmployee.GetById(glkDeliverer.EditValue.ToString());
            this.txtDelivererName.Text = objEmployee.firstname + @" " + objEmployee.lastname;
        }

        private void glkEmployee_EditValueChanged(object sender, EventArgs e)
        {
            hrm_employee objEmployee = _exeDicEmployee.GetById(glkEmployee.EditValue.ToString());
            this.txtEmployeeName.Text = objEmployee.firstname + @" " + objEmployee.lastname;
        }
    }
}
